package com.java.common.utils;

import lombok.Data;

@Data

public class R<T> {

    private Integer code;
    private String message;
    private T data;


    private R(){

    }
    //封装返回数据
    public static<T> R<T> build(T data, ResultCodeEnum resultCodeEnum){
        R result = new R();
        if(data!=null){
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    //成功,200
    public static<T> R<T> ok(){
        return build(null,ResultCodeEnum.SUCCESS);
    }

    public static<T> R<T> ok(T data){
        return build(data,ResultCodeEnum.SUCCESS);
    }


    //失败,201
    public static<T> R<T> fail(){
        return build(null,ResultCodeEnum.FAIL);
    }

    public static<T> R<T> fail(T data){
        return build(data,ResultCodeEnum.FAIL);
    }

    //自定义返回信息
    public R<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public R<T> message(String msg){
        this.setMessage(msg);
        return this;
    }
}
